package com.new_wear.project.system.group.controller;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.new_wear.common.exception.BusinessException;
import com.new_wear.common.exception.base.BaseException;
import com.new_wear.framework.web.domain.Ztree;
import com.new_wear.project.system.menu.service.MenuServiceImpl;
import com.new_wear.project.system.role.domain.Role;
import com.new_wear.project.system.volunteer.domain.CstVolunteer;
import com.new_wear.project.system.volunteer.service.impl.CstVolunteerServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.system.group.domain.CstVolunteerGroup;
import com.new_wear.project.system.group.service.ICstVolunteerGroupService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;

/**
 * 志愿者 - 志愿者团体Controller
 * 
 * @author new_wear
 * @date 2019-12-31
 */
@Controller
@RequestMapping("/system/group")
public class CstVolunteerGroupController extends BaseController
{
    private String prefix = "system/group";

    @Autowired
    private ICstVolunteerGroupService cstVolunteerGroupService;
    @Autowired
    private MenuServiceImpl menuService;
    @Autowired
    private CstVolunteerServiceImpl cstVolunteerService;

    @RequiresPermissions("system:group:view")
    @GetMapping()
    public String group()
    {
        return prefix + "/group";
    }

    /**
     * 查询志愿者 - 志愿者团体列表
     */
    @RequiresPermissions("system:group:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CstVolunteerGroup> list(CstVolunteerGroup cstVolunteerGroup)
    {
        List<CstVolunteerGroup> list = cstVolunteerGroupService.selectCstVolunteerGroupList(cstVolunteerGroup);
        for (CstVolunteerGroup group:list) {
            /*根据团体id，查询志愿者关联人数*/
            CstVolunteer cstVolunteer = new CstVolunteer();
            cstVolunteer.setGid(group.getId());
            List<CstVolunteer> cstVolunteers = cstVolunteerService.selectCstVolunteerList(cstVolunteer);
            group.setVolunteerNuber(cstVolunteers.size());
        }
        return list;
    }

    /**
     * 导出志愿者 - 志愿者团体列表
     */
    @RequiresPermissions("system:group:export")
    @Log(title = "志愿者 - 志愿者团体", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstVolunteerGroup cstVolunteerGroup)
    {
        List<CstVolunteerGroup> list = cstVolunteerGroupService.selectCstVolunteerGroupList(cstVolunteerGroup);
        ExcelUtil<CstVolunteerGroup> util = new ExcelUtil<CstVolunteerGroup>(CstVolunteerGroup.class);
        return util.exportExcel(list, "group");
    }

    /**
     * 新增志愿者 - 志愿者团体
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存志愿者 - 志愿者团体
     */
    @RequiresPermissions("system:group:add")
    @Log(title = "志愿者 - 志愿者团体", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CstVolunteerGroup cstVolunteerGroup)
    {
        cstVolunteerGroup.setParentId(0);
        return toAjax(cstVolunteerGroupService.insertCstVolunteerGroup(cstVolunteerGroup));
    }

    /**
     * 修改志愿者 - 志愿者团体
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {

        CstVolunteerGroup cstVolunteerGroup = cstVolunteerGroupService.selectCstVolunteerGroupById(id);
        mmap.put("cstVolunteerGroup", cstVolunteerGroup);
        return prefix + "/edit";
    }

    /**
     * 修改保存志愿者 - 志愿者团体
     */
    @RequiresPermissions("system:group:edit")
    @Log(title = "志愿者 - 志愿者团体", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated CstVolunteerGroup cstVolunteerGroup)
    {
        return toAjax(cstVolunteerGroupService.updateCstVolunteerGroup(cstVolunteerGroup));
    }

    /**
     * 删除志愿者 - 志愿者团体
     */
    @RequiresPermissions("system:group:remove")
    @Log(title = "志愿者 - 志愿者团体", businessType = BusinessType.DELETE)
    @GetMapping( "/remove/{menuId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("menuId") Long id)
    {
        return toAjax(cstVolunteerGroupService.deleteCstVolunteerGroupByIds(id.toString()));
    }


    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Ztree> roleMenuTreeData(Role role)
    {
        List<Ztree> ztrees = menuService.roleMenuTreeData(role);
        return ztrees;
    }

    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<Ztree> menuTreeData(Role role)
    {
        List<Ztree> ztrees = cstVolunteerGroupService.menuTreeData();
        return ztrees;
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree/{id}")
    public String selectMenuTree(@PathVariable("id") Integer id, ModelMap mmap)
    {

        mmap.put("group", cstVolunteerGroupService.selectCstVolunteerGroupById(id));
        return prefix + "/tree";
    }

    @GetMapping("/sublevelAdd/{id}")
    public String sublevelAdd(@PathVariable("id") Integer id,ModelMap map){
        map.put("id",id);
        return prefix +"/sublevelAdd";
    }

    @RequestMapping("/addSublevel")
    @ResponseBody
    public AjaxResult addSublevel(@RequestParam("parentId") Integer parentId , CstVolunteerGroup cstVolunteerGroup){
        cstVolunteerGroup.setParentId(parentId);
        return toAjax(cstVolunteerGroupService.insertCstVolunteerGroup(cstVolunteerGroup));
    }
}




